﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Security;
using System.Windows.Forms;

namespace Lib
{
    public class StringUtil
    {
        /// <summary>
        /// 检查str != null && !str.ToString().Trim().Equals("")
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool isNotEmpty(String str)
        {
            if (str != null && !str.ToString().Trim().Equals(""))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 检查obj != null && !obj.ToString().Trim().Equals("")
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool isNotEmpty(Object obj)
        {
            if (obj != null && !obj.ToString().Trim().Equals(""))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 检查字符串是否是double数字
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool isDouble(String str)
        {
            try
            {
                double.Parse(str);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 检查字符串是否是int类型
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool isInt(String str)
        {
            try
            {
                int.Parse(str);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 返回字符串MD5加密后密文
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static String getMD5(String str)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
        }
        
        /// <summary>
        /// 对double四舍五入，返回2位小数
        /// </summary>
        /// <param name="dvalue"></param>
        /// <returns></returns>
        public static string toStringRound(double dvalue)
        {
            return Math.Round(dvalue, 2, MidpointRounding.AwayFromZero).ToString();
        }

        /// <summary>
        /// objValue如果为空则返回0，否则objValue数字转为double，然后四舍五入
        /// </summary>
        /// <param name="objValue"></param>
        /// <returns></returns>
        public static string toStringRound(Object objValue)
        {
            string number = nullTozero(objValue);
            return Math.Round(double.Parse(number), 2, MidpointRounding.AwayFromZero).ToString();
        }

        /// <summary>
        /// null对象转空字符串
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string nullTonothing(Object value)
        {
            if (value == null)
            {
                return "";
            }
            return value.ToString();
        }

        /// <summary>
        /// 如果值为null或空字符串，则返回0
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string nullTozero(Object value)
        {
            if (value == null || value.ToString().Equals(""))
            {
                return "0";
            }
            return value.ToString();
        }

        /// <summary>
        /// 判断dgv的cellvalue是有效的数字且不为空
        /// </summary>
        /// <param name="cellValue"></param>
        /// <returns></returns>
        public static bool checkCellValueDouble(object cellValue)
        {
            if (cellValue != null && !cellValue.ToString().Equals("") && isDouble(cellValue.ToString()))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 日期字符串格式化
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string formatDate(string date)
        {
            date = date.Replace('/', '-').Replace('\\', '-');
            DateTime dt = Convert.ToDateTime(date);
            return dt.ToString("yyyy-MM-dd");
        }

        /// <summary>
        /// 获取当前时间戳
        /// </summary>
        /// <returns>long(秒)</returns>
        public static long getCurrentDateTimeLong()
        {
            return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
        }

        /// <summary>
        /// 通过选择的时间生成long类型日期
        /// </summary>
        /// <param name="dateTimePicker">dateTimePicker日期控件</param>
        /// <returns></returns>
        public static long getDateTimeLong(DateTimePicker dateTimePicker)
        {
            string dateStr = dateTimePicker.Text + " " + DateTime.Now.ToString("HH:mm:ss");
            return (Convert.ToDateTime(dateStr).ToUniversalTime().Ticks - 621355968000000000) / 10000000;
        }
    }
}
